<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>我的订单</span>
        </div>
      </template>

      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input
            v-model="queryParams.orderNo"
            placeholder="请输入订单编号"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable>
            <el-option
              v-for="dict in orderStatusOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="orderList"
        style="width: 100%"
      >
        <el-table-column prop="orderNo" label="订单编号" width="180" />
        <el-table-column label="商品信息">
          <template #default="scope">
            <div v-for="item in scope.row.items" :key="item.id" class="order-item">
              <el-image 
                style="width: 50px; height: 50px"
                :src="item.productImage"
                fit="cover"
              />
              <span class="product-name">{{ item.productName }}</span>
              <span class="quantity">x{{ item.quantity }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="totalAmount" label="订单金额" width="120">
          <template #default="scope">
            ¥{{ scope.row.totalAmount }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="订单状态" width="120">
          <template #default="scope">
            <el-tag :type="getOrderStatusType(scope.row.status)">
              {{ getOrderStatusLabel(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="下单时间" width="180" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
            <el-button
              v-if="scope.row.status === '1'"
              link
              type="primary"
              @click="handlePay(scope.row)"
            >
              付款
            </el-button>
            <el-button
              v-if="scope.row.status === '1'"
              link
              type="danger"
              @click="handleCancel(scope.row)"
            >
              取消
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()
const loading = ref(false)
const total = ref(0)
const orderList = ref([])
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  orderNo: undefined,
  status: undefined
})

// 订单状态选项
const orderStatusOptions = [
  { value: '1', label: '待付款' },
  { value: '2', label: '待发货' },
  { value: '3', label: '待收货' },
  { value: '4', label: '已完成' },
  { value: '5', label: '已取消' }
]

// 获取订单状态标签
const getOrderStatusLabel = (status) => {
  const option = orderStatusOptions.find(item => item.value === status)
  return option ? option.label : ''
}

// 获取订单状态标签类型
const getOrderStatusType = (status) => {
  switch (status) {
    case '1': return 'warning'
    case '2': return 'primary'
    case '3': return 'success'
    case '4': return 'info'
    case '5': return 'danger'
    default: return 'info'
  }
}

// 查询订单列表
const getList = () => {
  loading.value = true
  // TODO: 调用后端接口获取订单列表
  loading.value = false
}

// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1
  getList()
}

// 重置
const resetQuery = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    orderNo: undefined,
    status: undefined
  }
  getList()
}

// 查看订单详情
const handleDetail = (row) => {
  router.push(`/mall/order/detail/${row.orderId}`)
}

// 支付订单
const handlePay = (row) => {
  // TODO: 跳转到支付页面
  router.push(`/mall/order/pay/${row.orderId}`)
}

// 取消订单
const handleCancel = (row) => {
  ElMessageBox.confirm(
    '确认取消该订单?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // TODO: 调用后端接口取消订单
    ElMessage.success('订单已取消')
    getList()
  }).catch(() => {})
}

// 初始化
getList()
</script>

<style scoped>
.order-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.order-item:last-child {
  margin-bottom: 0;
}

.product-name {
  margin-left: 10px;
  flex: 1;
}

.quantity {
  color: #999;
  margin-left: 10px;
}
</style> 